package com.ethan.cj.ssm.service.impl;

import com.ethan.cj.ssm.dao.LogsDao;
import com.ethan.cj.ssm.entity.Logs;
import com.ethan.cj.ssm.service.LogsService;
import com.ethan.cj.ssm.util.PageBean;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志逻辑处理层接口的实现
 */
@Service
public class LogsServiceImpl implements LogsService {

    /**
     * 日志dao层映射
     */
    @Autowired
    private LogsDao ld;

    /**
     * 添加一条日志信息
     * @param logs 日志对象
     */
    @Override
    public void insertLogs(Logs logs) {
        ld.insertLogs(logs);
    }

    /**
     * 分页查询
     * @param pb PageBean<Logs> 对象
     * @param params 查询条件
     * @return PageBean<Logs> 对象
     */
    @Override
    public Map<Object,Object> selectLogs(PageBean<Logs> pb, String params) {
        JSONObject param1 = new JSONObject(params);
        String lrunner = param1.getString("lrunner");
        String lip = param1.getString("lip");
        String loperation = param1.getString("loperation");
        int count = ld.count(lrunner, lip, loperation);
        List<Logs> list = ld.selectLogs(lrunner, lip, loperation,pb.getOffset(),pb.getPageSize());
        pb.setList(list);
        pb.setTotalCount(count);
        Map<Object,Object> result = new HashMap<>();
        if (list != null && list.size() > 0) {
            result.put("code", 0);
            result.put("msg", "");
            result.put("data", list);
            result.put("count", count);
        } else {
            result.put("code", 1);
            result.put("msg", "无数据");
        }
        return result;
    }

    /**
     * 查询所有日志信息
     * @return 日志 json对象
     */
    @Override
    public Map<Object,Object> logsList() {
        List<Logs> list = ld.logsList();
        Map<Object,Object> result = new HashMap<>();
        if (list != null && list.size() > 0) {
            result.put("code", 0);
            result.put("msg", "");
            result.put("data", list);
        } else {
            result.put("code", 1);
            result.put("msg", "无数据");
        }
        return result;
    }


}
